package com.guo.pet_store.admin.dao;

import com.guo.pet_store.admin.pojo.Pet;
import com.guo.pet_store.admin.pojo.PetQuery;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

public interface PetRepository extends JpaRepository<Pet,Long>, JpaSpecificationExecutor<Pet> {

    Pet findPetById(Long id);
    @Query("select b from Pet b where b.type.id = ?1")
    List<Pet> findPetTypes(Long id);

    @Transactional
    @Modifying
    @Query(nativeQuery = true,value = "update pet set on_sale = 0 , delete_date = ?2 where id = ?1")
    int deletePet(Long petId, String dateAndTime);

    @Transactional
    @Modifying
    @Query(nativeQuery = true,value = "update pet_shop_car set pet_counts = ?3  where pet_id = ?1 and shop_car_id = ?2")
    void setCount(int pet_id,long shopCarid,int count);

    @Transactional
    @Query(nativeQuery = true,value = "select * from pet where on_sale = 1")
    List<Pet> selectPetsOnSale();

//    查询方法
    @Query(nativeQuery = true,value = "select * from pet where (description like ?2 or age=?1 or male=?1 or name like ?2) and on_sale = 1")
    List<Pet> searchPet(String search,String likeSearch);

//    查询所有父Id的宠物
@Query(nativeQuery = true,value = "SELECT * FROM pet \n" +
        "JOIN TYPE\n" +
        "ON pet.type_id = type.id \n" +
        "WHERE type.parent_id = ?1 AND pet.on_sale = 1;")
List<Pet> petsByParentId(Long parentTypeId);

@Query(nativeQuery = true,value = "select pet_counts from pet_shop_car where shop_car_id = ?1")
    List<Integer> getCarnumbers(Long carId);



}
